﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Dry.DbInterpreter;

namespace Dry
{
    public static class Dry
    {
        public static string ConnectionString { get; set; }
        public static string ProjectName { get; set; }
        public static string ProjectPath { get; set; }

        public static GeneratorFeedback ValidateTableForBackend(string strTableName, string tablePrefix)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            DbTable dbTable = new DbTable(strTableName, tablePrefix);

            foreach (DbColumn dbColumn in dbTable.DbColumns)
            {
                if(dbColumn.IsFK)
                {
                    // Check that the name created for the reference class don't already exists in an other column
                    // If a tabel has the following columns
                    // nvarchar  OrderStatus
                    // int       OrderStatusId
                    // OrderStatusId will try to create a property called OrderStatus that will conflict with the existing column
                    foreach (DbColumn dbCompareColumn in dbTable.DbColumns)
                        if (dbColumn.ReferenceClass == dbCompareColumn.ColumnName)
                        {
                            generatorFeedback.Exception = new Exception("Ambiguous name for reference class to column " + dbColumn.ColumnName + ", name already exsits in column " + dbCompareColumn.ColumnName + " for table " + dbTable.TableName);
                            break;
                        }

                    if(generatorFeedback.HasError)
                        break;
                }
            }

            return generatorFeedback;
        }

        public static GeneratorFeedback ValidateTableForFrontend(string strTableName, string tablePrefix)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            DbTable dbTable = new DbTable(strTableName, tablePrefix);

            foreach (DbColumn dbColumn in dbTable.DbColumns)
            {
                
            }

            return generatorFeedback;
        }

        public static void GenerateBackend()
        {
            if(!Directory.Exists(Dry.ProjectPath + "\\BO"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\BO");
            
            if(!Directory.Exists(Dry.ProjectPath + "\\BO\\AutoGenerated"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\BO\\AutoGenerated");

            if (!Directory.Exists(Dry.ProjectPath + "\\BLL"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\BLL");

            if (!Directory.Exists(Dry.ProjectPath + "\\BLL\\AutoGenerated"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\BLL\\AutoGenerated");

            if (!Directory.Exists(Dry.ProjectPath + "\\IP"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\IP");

            if(!Directory.Exists(Dry.ProjectPath + "\\IP\\AutoGenerated"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\IP\\AutoGenerated");

            if (!Directory.Exists(Dry.ProjectPath + "\\DAL"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\DAL");

            if(!Directory.Exists(Dry.ProjectPath + "\\DAL\\AutoGenerated"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\DAL\\AutoGenerated");

            if (!Directory.Exists(Dry.ProjectPath + "\\Enums"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\Enums");

            if (!Directory.Exists(Dry.ProjectPath + "\\Enums\\AutoGenerated"))
                Directory.CreateDirectory(Dry.ProjectPath + "\\Enums\\AutoGenerated");
            
            GenerateBO();
            GenerateIP();
            GenerateDAL();
            GenerateBLL();
            GenerateEnums();
        }
        
        public static void GenerateBO()
        {
            Generators.BOGenerator.CreateBaseBO();

            foreach (DbInterpreter.DbTable dbTable in DbInterpreter.DbTable.GetAllDbTables())
            {
                    Generators.BOGenerator.CreateFile(dbTable.TableName, false);
            }
        }

        public static void GenerateIP()
        {
            Generators.IPGenerator.CreateBaseIP();

            foreach (DbInterpreter.DbTable dbTable in DbInterpreter.DbTable.GetAllDbTables())
            {
                    Generators.IPGenerator.CreateFile(dbTable.TableName);
            }
        }

        public static void GenerateBLL()
        {
            Generators.BLLGenerator.CreateBaseBLL();

            foreach (DbInterpreter.DbTable dbTable in DbInterpreter.DbTable.GetAllDbTables())
            {
                    Generators.BLLGenerator.CreateFile(dbTable.TableName, false);
            }
        }

        public static void GenerateDAL()
        {
            Generators.DALGenerator.CreateBaseDAL();

            foreach (DbInterpreter.DbTable dbTable in DbInterpreter.DbTable.GetAllDbTables())
            {
                    Generators.DALGenerator.CreateFile(dbTable.TableName, "4", false);
            }
        }

        public static void GenerateEnums()
        {
            foreach (DbInterpreter.DbTable dbTable in DbInterpreter.DbTable.GetAllDbTables())
            {
                if(dbTable.TableName.ToString().EndsWith("Enum"))
                    Generators.EnumGenerator.CreateFile(dbTable.TableName);
            }
        }
    }
}
